stream-markdown-parser 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md ADDED
@@ -0,0 +1,97 @@
1
+
2
+ # stream-markdown-parser
3
+
4
+ Lightweight, framework-agnostic Markdown parser implemented in TypeScript. It focuses on producing a typed, structured representation of Markdown content so you can render it in Vue, React, or any other environment.
5
+
6
+ [![NPM version](https://img.shields.io/npm/v/stream-markdown-parser?color=a1b858&label=)](https://www.npmjs.com/package/stream-markdown-parser)
7
+ [![中文版](https://img.shields.io/badge/docs-中文文档-blue)](README.zh-CN.md)
8
+ [![NPM downloads](https://img.shields.io/npm/dm/stream-markdown-parser)](https://www.npmjs.com/package/stream-markdown-parser)
9
+ [![Bundle size](https://img.shields.io/bundlephobia/minzip/stream-markdown-parser)](https://bundlephobia.com/package/stream-markdown-parser)
10
+ [![License](https://img.shields.io/npm/l/stream-markdown-parser)](./LICENSE)
11
+
12
+ ## Highlights
13
+
14
+ - Framework-agnostic: pure TypeScript, no runtime framework dependency
15
+ - Typed output: full TypeScript definitions for the produced node tree
16
+ - Extensible: built on top of markdown-it and supports plugins (math, containers, etc.)
17
+ - Optimized for streaming / large documents
18
+
19
+ ## Install
20
+
21
+ ```bash
22
+ npm install stream-markdown-parser
23
+ # or
24
+ pnpm add stream-markdown-parser
25
+ ```
26
+
27
+ ## Quickstart
28
+
29
+ ```ts
30
+ import { getMarkdown, parseMarkdownToStructure } from 'stream-markdown-parser'
31
+
32
+ const md = getMarkdown()
33
+ const nodes = parseMarkdownToStructure('# Hello World', md)
34
+ console.log(nodes)
35
+ ```
36
+
37
+ ## Examples
38
+
39
+ Basic parsing, math and custom container usage:
40
+
41
+ ```ts
42
+ // basic
43
+ const md = getMarkdown()
44
+ parseMarkdownToStructure('# Title', md)
45
+
46
+ // enable math
47
+ const mdWithMath = getMarkdown({ enableMath: true })
48
+ parseMarkdownToStructure('Inline $x^2$ and block math:\n$$x^2$$', mdWithMath)
49
+
50
+ // enable custom containers
51
+ const mdWithContainers = getMarkdown({ enableContainers: true })
52
+ parseMarkdownToStructure('::: tip\nHi\n:::', mdWithContainers)
53
+ ```
54
+
55
+ You can also process tokens directly:
56
+
57
+ ```ts
58
+ import { processTokens } from 'stream-markdown-parser'
59
+ import MarkdownIt from 'markdown-it'
60
+
61
+ const md = new MarkdownIt()
62
+ const tokens = md.parse('# Hello', {})
63
+ const nodes = processTokens(tokens)
64
+ ```
65
+
66
+ ## API (overview)
67
+
68
+ - getMarkdown(options?): returns a configured markdown-it instance
69
+ - parseMarkdownToStructure(markdown, md, options?): parse a markdown string into typed nodes
70
+ - processTokens(tokens): convert markdown-it tokens into typed nodes
71
+ - parseInlineTokens(tokens): parse inline tokens
72
+
73
+ See `src/types/index.ts` for the full `ParsedNode` definitions.
74
+
75
+ ## Integration
76
+
77
+ - React / Vue / Vanilla: parse to nodes, then render with your components
78
+
79
+ ## Build & Dev
80
+
81
+ This package is built using Vite and `vite-plugin-dts` for types generation. In this monorepo the package is under `packages/parser`.
82
+
83
+ Build locally:
84
+
85
+ ```bash
86
+ pnpm --filter ./packages/parser build
87
+ ```
88
+
89
+ Typecheck:
90
+
91
+ ```bash
92
+ pnpm --filter ./packages/parser -w -C . typecheck
93
+ ```
94
+
95
+ ## License
96
+
97
+ MIT
@@ -0,0 +1,96 @@
1
+ # stream-markdown-parser
2
+
3
+ 轻量化、框架无关的 Markdown 解析器,使用 TypeScript 编写。输出为强类型的节点树,方便在 Vue、React 或任何运行时中渲染。
4
+
5
+ [![NPM version](https://img.shields.io/npm/v/stream-markdown-parser?color=a1b858&label=)](https://www.npmjs.com/package/stream-markdown-parser)
6
+ [![English Docs](https://img.shields.io/badge/docs-English-blue)](README.md)
7
+ [![NPM downloads](https://img.shields.io/npm/dm/stream-markdown-parser)](https://www.npmjs.com/package/stream-markdown-parser)
8
+ [![Bundle size](https://img.shields.io/bundlephobia/minzip/stream-markdown-parser)](https://bundlephobia.com/package/stream-markdown-parser)
9
+ [![License](https://img.shields.io/npm/l/stream-markdown-parser)](./LICENSE)
10
+
11
+ ## 主要特点
12
+
13
+ - 框架无关:纯 TypeScript 实现,无运行时框架依赖
14
+ - 强类型输出:包含完整的 TypeScript 类型定义
15
+ - 可扩展:基于 markdown-it,支持插件(例如数学公式、容器等)
16
+ - 适用于流式或大文档解析场景
17
+
18
+ ## 安装
19
+
20
+ ```bash
21
+ pnpm add stream-markdown-parser
22
+ # 或
23
+ npm install stream-markdown-parser
24
+ ```
25
+
26
+ ## 快速开始
27
+
28
+ ```ts
29
+ import { getMarkdown, parseMarkdownToStructure } from 'stream-markdown-parser'
30
+
31
+ const md = getMarkdown()
32
+ const nodes = parseMarkdownToStructure('# Hello World', md)
33
+ console.log(nodes)
34
+ ```
35
+
36
+ ## 示例
37
+
38
+ 基本解析、数学公式与自定义容器:
39
+
40
+ ```ts
41
+ // 基本用法
42
+ const md = getMarkdown()
43
+ parseMarkdownToStructure('# 标题', md)
44
+
45
+ // 启用数学公式
46
+ const mdWithMath = getMarkdown({ enableMath: true })
47
+ parseMarkdownToStructure('行内 $x^2$ 与 块级公式:\n$$x^2$$', mdWithMath)
48
+
49
+ // 启用自定义容器
50
+ const mdWithContainers = getMarkdown({ enableContainers: true })
51
+ parseMarkdownToStructure('::: tip\n提示内容\n:::', mdWithContainers)
52
+ ```
53
+
54
+ 也可以直接处理 markdown-it 的 tokens:
55
+
56
+ ```ts
57
+ import MarkdownIt from 'markdown-it'
58
+ import { processTokens } from 'stream-markdown-parser'
59
+
60
+ const md = new MarkdownIt()
61
+ const tokens = md.parse('# Hello', {})
62
+ const nodes = processTokens(tokens)
63
+ ```
64
+
65
+ ## API 概览
66
+
67
+ - `getMarkdown(options?)`:返回配置好的 `markdown-it` 实例
68
+ - `parseMarkdownToStructure(markdown, md, options?)`:将 markdown 字符串解析为类型化节点
69
+ - `processTokens(tokens)`:将 markdown-it tokens 转换为节点
70
+ - `parseInlineTokens(tokens)`:解析内联 tokens
71
+
72
+ 完整的节点类型定义请查看 `src/types/index.ts`。
73
+
74
+ ## 与不同框架的集成
75
+
76
+ 解析出节点后,在 React / Vue / 或原生 JS 中用你的组件或渲染逻辑对节点进行渲染即可。
77
+
78
+ ## 构建与开发
79
+
80
+ 该包使用 Vite 构建,并通过 `vite-plugin-dts` 生成声明文件,在 monorepo 中位于 `packages/parser`。
81
+
82
+ 本地构建:
83
+
84
+ ```bash
85
+ pnpm --filter ./packages/parser build
86
+ ```
87
+
88
+ 类型检查:
89
+
90
+ ```bash
91
+ pnpm --filter ./packages/parser -w -C . typecheck
92
+ ```
93
+
94
+ ## 许可证
95
+
96
+ MIT
@@ -0,0 +1,323 @@
1
+ import { default as default_2 } from 'markdown-it';
2
+
3
+ export declare interface AdmonitionNode extends BaseNode {
4
+ type: 'admonition';
5
+ kind: string;
6
+ title: string;
7
+ children: ParsedNode[];
8
+ }
9
+
10
+ export declare function applyContainers(md: default_2): void;
11
+
12
+ export declare function applyMath(md: default_2, mathOpts?: MathOptions): void;
13
+
14
+ export declare interface BaseNode {
15
+ type: string;
16
+ raw: string;
17
+ loading?: boolean;
18
+ code?: string;
19
+ diff?: boolean;
20
+ }
21
+
22
+ export declare interface BlockquoteNode extends BaseNode {
23
+ type: 'blockquote';
24
+ children: ParsedNode[];
25
+ }
26
+
27
+ export declare interface CheckboxInputNode extends BaseNode {
28
+ type: 'checkbox_input';
29
+ checked: boolean;
30
+ }
31
+
32
+ export declare interface CheckboxNode extends BaseNode {
33
+ type: 'checkbox';
34
+ checked: boolean;
35
+ }
36
+
37
+ export declare interface CodeBlockNode extends BaseNode {
38
+ type: 'code_block';
39
+ language: string;
40
+ code: string;
41
+ startLine?: number;
42
+ endLine?: number;
43
+ loading?: boolean;
44
+ diff?: boolean;
45
+ originalCode?: string;
46
+ updatedCode?: string;
47
+ raw: string;
48
+ }
49
+
50
+ export declare interface CustomComponents {
51
+ text: any;
52
+ paragraph: any;
53
+ heading: any;
54
+ code_block: any;
55
+ list: any;
56
+ blockquote: any;
57
+ table: any;
58
+ definition_list: any;
59
+ footnote: any;
60
+ footnote_reference: any;
61
+ admonition: any;
62
+ hardbreak: any;
63
+ link: any;
64
+ image: any;
65
+ thematic_break: any;
66
+ math_inline: any;
67
+ math_block: any;
68
+ strong: any;
69
+ emphasis: any;
70
+ strikethrough: any;
71
+ highlight: any;
72
+ insert: any;
73
+ subscript: any;
74
+ superscript: any;
75
+ emoji: any;
76
+ checkbox: any;
77
+ inline_code: any;
78
+ reference: any;
79
+ mermaid: any;
80
+ [key: string]: any;
81
+ }
82
+
83
+ export declare interface DefinitionItemNode extends BaseNode {
84
+ type: 'definition_item';
85
+ term: ParsedNode[];
86
+ definition: ParsedNode[];
87
+ }
88
+
89
+ export declare interface DefinitionListNode extends BaseNode {
90
+ type: 'definition_list';
91
+ items: DefinitionItemNode[];
92
+ }
93
+
94
+ export declare interface EmojiNode extends BaseNode {
95
+ type: 'emoji';
96
+ name: string;
97
+ markup: string;
98
+ }
99
+
100
+ export declare interface EmphasisNode extends BaseNode {
101
+ type: 'emphasis';
102
+ children: ParsedNode[];
103
+ }
104
+
105
+ export declare function findMatchingClose(src: string, startIdx: number, open: string, close: string): number;
106
+
107
+ export declare interface FootnoteNode extends BaseNode {
108
+ type: 'footnote';
109
+ id: string;
110
+ children: ParsedNode[];
111
+ }
112
+
113
+ export declare interface FootnoteReferenceNode extends BaseNode {
114
+ type: 'footnote_reference';
115
+ id: string;
116
+ }
117
+
118
+ export declare function getDefaultMathOptions(): MathOptions | undefined;
119
+
120
+ export declare function getMarkdown(opts?: GetMarkdownOptions): default_2;
121
+
122
+ export declare interface GetMarkdownOptions extends Record<string, any> {
123
+ markdownItOptions?: Record<string, any>;
124
+ enableMath?: boolean;
125
+ enableContainers?: boolean;
126
+ mathOptions?: {
127
+ commands?: string[];
128
+ escapeExclamation?: boolean;
129
+ };
130
+ }
131
+
132
+ export declare interface HardBreakNode extends BaseNode {
133
+ type: 'hardbreak';
134
+ }
135
+
136
+ export declare interface HeadingNode extends BaseNode {
137
+ type: 'heading';
138
+ level: number;
139
+ text: string;
140
+ children: ParsedNode[];
141
+ }
142
+
143
+ export declare interface HighlightNode extends BaseNode {
144
+ type: 'highlight';
145
+ children: ParsedNode[];
146
+ }
147
+
148
+ export declare interface ImageNode extends BaseNode {
149
+ type: 'image';
150
+ src: string;
151
+ alt: string;
152
+ title: string | null;
153
+ }
154
+
155
+ export declare interface InlineCodeNode extends BaseNode {
156
+ type: 'inline_code';
157
+ code: string;
158
+ }
159
+
160
+ export declare interface InsertNode extends BaseNode {
161
+ type: 'insert';
162
+ children: ParsedNode[];
163
+ }
164
+
165
+ export declare const KATEX_COMMANDS: string[];
166
+
167
+ export declare interface LinkNode extends BaseNode {
168
+ type: 'link';
169
+ href: string;
170
+ title: string | null;
171
+ text: string;
172
+ children: ParsedNode[];
173
+ }
174
+
175
+ export declare interface ListItemNode extends BaseNode {
176
+ type: 'list_item';
177
+ children: ParsedNode[];
178
+ }
179
+
180
+ export declare interface ListNode extends BaseNode {
181
+ type: 'list';
182
+ ordered: boolean;
183
+ start?: number;
184
+ items: ListItemNode[];
185
+ }
186
+
187
+ export declare type MarkdownRender = {
188
+ content: string;
189
+ nodes?: undefined;
190
+ } | {
191
+ content?: undefined;
192
+ nodes: BaseNode[];
193
+ };
194
+
195
+ export declare interface MarkdownToken {
196
+ type: string;
197
+ tag?: string;
198
+ content?: string;
199
+ info?: string;
200
+ loading?: boolean;
201
+ children?: MarkdownToken[];
202
+ attrs?: [string, string][];
203
+ markup?: string;
204
+ meta?: any;
205
+ map?: [number, number];
206
+ raw?: string;
207
+ }
208
+
209
+ export declare interface MathBlockNode extends BaseNode {
210
+ type: 'math_block';
211
+ content: string;
212
+ }
213
+
214
+ export declare interface MathInlineNode extends BaseNode {
215
+ type: 'math_inline';
216
+ content: string;
217
+ }
218
+
219
+ /**
220
+ * MathOptions control how the math plugin normalizes content before
221
+ * handing it to KaTeX (or other math renderers).
222
+ *
223
+ * - commands: list of command words that should be auto-prefixed with a
224
+ * backslash if not already escaped (e.g. 'infty' -> '\\infty'). Use a
225
+ * conservative list to avoid false positives in prose.
226
+ * - escapeExclamation: whether to escape standalone '!' to '\\!' (default true).
227
+ */
228
+ export declare interface MathOptions {
229
+ /** List of command words to auto-escape. */
230
+ commands?: readonly string[];
231
+ /** Whether to escape standalone '!' (default: true). */
232
+ escapeExclamation?: boolean;
233
+ }
234
+
235
+ export declare interface MermaidBlockNode {
236
+ node: {
237
+ type: 'code_block';
238
+ language: string;
239
+ code: string;
240
+ loading?: boolean;
241
+ };
242
+ }
243
+
244
+ export declare function normalizeStandaloneBackslashT(s: string, opts?: MathOptions): string;
245
+
246
+ export declare interface ParagraphNode extends BaseNode {
247
+ type: 'paragraph';
248
+ children: ParsedNode[];
249
+ maybeCheckbox?: boolean;
250
+ }
251
+
252
+ export declare type ParsedNode = TextNode | HeadingNode | ParagraphNode | ListNode | ListItemNode | CodeBlockNode | InlineCodeNode | LinkNode | ImageNode | ThematicBreakNode | BlockquoteNode | TableNode | TableRowNode | TableCellNode | StrongNode | EmphasisNode | StrikethroughNode | HighlightNode | InsertNode | SubscriptNode | SuperscriptNode | CheckboxNode | CheckboxInputNode | EmojiNode | DefinitionListNode | DefinitionItemNode | FootnoteNode | FootnoteReferenceNode | AdmonitionNode | HardBreakNode | MathInlineNode | MathBlockNode | ReferenceNode | Record<string, any>;
253
+
254
+ export declare function parseInlineTokens(tokens: MarkdownToken[], raw?: string, pPreToken?: MarkdownToken): ParsedNode[];
255
+
256
+ export declare function parseMarkdownToStructure(markdown: string, md: default_2, options?: ParseOptions): ParsedNode[];
257
+
258
+ export declare interface ParseOptions {
259
+ preTransformTokens?: TransformTokensHook;
260
+ postTransformTokens?: TransformTokensHook;
261
+ }
262
+
263
+ export declare type PostTransformNodesHook = (nodes: ParsedNode[]) => ParsedNode[];
264
+
265
+ export declare function processTokens(tokens: MarkdownToken[]): ParsedNode[];
266
+
267
+ export declare interface ReferenceNode extends BaseNode {
268
+ type: 'reference';
269
+ id: string;
270
+ }
271
+
272
+ export declare function setDefaultMathOptions(opts: MathOptions | undefined): void;
273
+
274
+ export declare interface StrikethroughNode extends BaseNode {
275
+ type: 'strikethrough';
276
+ children: ParsedNode[];
277
+ }
278
+
279
+ export declare interface StrongNode extends BaseNode {
280
+ type: 'strong';
281
+ children: ParsedNode[];
282
+ }
283
+
284
+ export declare interface SubscriptNode extends BaseNode {
285
+ type: 'subscript';
286
+ children: ParsedNode[];
287
+ }
288
+
289
+ export declare interface SuperscriptNode extends BaseNode {
290
+ type: 'superscript';
291
+ children: ParsedNode[];
292
+ }
293
+
294
+ export declare interface TableCellNode extends BaseNode {
295
+ type: 'table_cell';
296
+ header: boolean;
297
+ children: ParsedNode[];
298
+ }
299
+
300
+ export declare interface TableNode extends BaseNode {
301
+ type: 'table';
302
+ header: TableRowNode;
303
+ rows: TableRowNode[];
304
+ }
305
+
306
+ export declare interface TableRowNode extends BaseNode {
307
+ type: 'table_row';
308
+ cells: TableCellNode[];
309
+ }
310
+
311
+ export declare interface TextNode extends BaseNode {
312
+ type: 'text';
313
+ content: string;
314
+ center?: boolean;
315
+ }
316
+
317
+ export declare interface ThematicBreakNode extends BaseNode {
318
+ type: 'thematic_break';
319
+ }
320
+
321
+ export declare type TransformTokensHook = (tokens: MarkdownToken[]) => MarkdownToken[];
322
+
323
+ export { }